package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Role;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.pojo.RolePojo;
import com.adminx.service.ResourceService;
import com.adminx.service.RoleService;
import com.adminx.util.CodeUtil;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;


@Controller
@RequestMapping("role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 初始化页面
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String init(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/role/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/role/init.html");
		return "admin/role/role";
	}
	
	/**
	 * 查询角色列表
	 * @param map
	 * @return 角色查询列表页面
	 */
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public Map<String, Object> query(Integer pageIndex, Integer pageSize, String searchText){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<RolePojo> result = this.roleService.findAllRole(pageIndex, pageSize, CodeUtil.transcoding(searchText));
			map.put("result", result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 通过角色ID获取角色
	 * @param roleID 角色ID
	 * @return 角色
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.GET) 
	public Map<String, Object> info(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			RolePojo rolePojo = this.roleService.info(id);
			map.put("rolePojo", rolePojo);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 保存或修改角色
	 * @param role 角色
	 * @return 角色
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST) 
	public Map<String, Object> save(Role role){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			this.roleService.save(role);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 删除角色
	 * @param roleID 角色ID
	 * @return 角色
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.GET) 
	public Map<String, Object> delBatch(String ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			int delNum = this.roleService.delBatch(ids);
			map.put("delNum", delNum);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 给角色赋权限
	 */
	@ResponseBody
	@RequestMapping(value = "/grantAuthority", method = RequestMethod.GET) 
	public Map<String, Object> grantAuthority(Long roleId,String authoritys){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			this.roleService.grantAuthority(roleId, authoritys);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
